package com.common;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.github.pagehelper.StringUtil;

public class DateUtlis {
    public static String YYYY_MM_DDHH_MM_SS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYYMM = "yyyyMMdd";

    public static String dateToString(Date date, String format) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
        return "";
    }

    public static Date stringToDate(String date, String format) throws ParseException {
        if (!StringUtil.isEmpty(date)) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date);
        }
        return null;
    }

    /**
     * 计算两个时间相差多少个年
     * 
     * @param early
     * @param late
     * @return
     * @throws ParseException
     */
    public static double yearsBetween(Date start, Date end) throws ParseException {
        int sumdays = daysBetween(start, end);
        DecimalFormat df = new DecimalFormat("#.00");
        return Double.valueOf(df.format((float)sumdays / 365));
    }

    /**
     * 计算两个时间相差多少个天
     * 
     * @param early
     * @param late
     * @return
     * @throws ParseException
     */
    public static int daysBetween(Date start, Date end) throws ParseException {
        // 得到两个日期相差多少天
        return hoursBetween(start, end) / 24;
    }

    /**
     * 计算两个时间相差多少小时
     * 
     * @param early
     * @param late
     * @return
     * @throws ParseException
     */
    public static int hoursBetween(Date start, Date end) throws ParseException {
        // 得到两个日期相差多少小时
        return minutesBetween(start, end) / 60;
    }

    /**
     * 计算两个时间相差多少分
     * 
     * @param early
     * @param late
     * @return
     * @throws ParseException
     */
    public static int minutesBetween(Date start, Date end) throws ParseException {
        // 得到两个日期相差多少分
        return secondesBetween(start, end) / 60;
    }

    /**
     * 计算两个时间相差多少秒
     * 
     * @param early
     * @param late
     * @return
     * @throws ParseException
     */
    public static int secondesBetween(Date start, Date end) throws ParseException {
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(start);
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(end);

        // 设置时间为0时
        startDate.set(Calendar.HOUR_OF_DAY, 0);
        startDate.set(Calendar.MINUTE, 0);
        startDate.set(Calendar.SECOND, 0);
        endDate.set(Calendar.HOUR_OF_DAY, 0);
        endDate.set(Calendar.MINUTE, 0);
        endDate.set(Calendar.SECOND, 0);
        // 得到两个日期相差多少秒
        return ((int) (endDate.getTime().getTime() / 1000) - (int) (startDate.getTime().getTime() / 1000));
    }
}
